//
//  XMData.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/23.
//

#import "XMData.h"

#import "XMFileManager.h"
#import <YYModel/YYModel.h>

#define JF_SINGLETON_IMP(_type_) + (_type_ *)shareInstance{\
static _type_ *theshareInstance = nil;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
theshareInstance = [[super alloc] init];\
});\
return theshareInstance;\
}

@interface XMData ()

@property (nonatomic, strong) XMCSProvinceModel *provincemodel;
@property (nonatomic, strong) XMHotCityModel *citymodel;
@property (nonatomic, strong) XMAreaModel *areaModel;

@end



@implementation XMData
JF_SINGLETON_IMP(XMData);

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}

- (void)initData {
    NSString *provincePlistPath = [[XMFileManager CitySelectorBundle] pathForResource:@"provinces" ofType:@"plist"];
    NSString *cityPlistPath = [[XMFileManager CitySelectorBundle] pathForResource:@"cities" ofType:@"plist"];
    NSString *areaPlistPath = [[XMFileManager CitySelectorBundle] pathForResource:@"areas" ofType:@"plist"];
    NSDictionary *provinceDic = [[NSDictionary alloc] initWithContentsOfFile:provincePlistPath];
    NSDictionary *cityDic = [[NSDictionary alloc] initWithContentsOfFile:cityPlistPath];
    NSDictionary *areaDic = [[NSDictionary alloc] initWithContentsOfFile:areaPlistPath];
    self.provincemodel = [XMCSProvinceModel yy_modelWithJSON:provinceDic];
    self.citymodel = [XMHotCityModel yy_modelWithJSON:cityDic];
    self.areaModel = [XMAreaModel yy_modelWithJSON:areaDic];
}

#pragma mark -- Get

- (NSArray <XMCSProvince *>*)provinces {
    if (!_provinces) {
        _provinces = [NSArray new];
        NSSortDescriptor *sortDes = [[NSSortDescriptor alloc] initWithKey:@"pinyin" ascending:YES];
        _provinces = [[self.provincemodel.provinces sortedArrayUsingDescriptors:@[sortDes]] mutableCopy];
    }
    return _provinces;
}

- (NSArray <XMHotCity *>*)cities {
    if (!_cities) {
        _cities = [NSArray new];
        NSSortDescriptor *sortDes = [[NSSortDescriptor alloc] initWithKey:@"pinyin" ascending:YES];
        _cities = [[self.citymodel.cities sortedArrayUsingDescriptors:@[sortDes]] mutableCopy];
    }
    return _cities;
}

- (NSArray <XMArea *>*)areas {
    if (!_areas) {
        _areas = [NSArray new];
        NSSortDescriptor *sortDes = [[NSSortDescriptor alloc] initWithKey:@"pinyin" ascending:YES];
        _areas = [[self.areaModel.areas sortedArrayUsingDescriptors:@[sortDes]] mutableCopy];
    }
    return _areas;
}

@end
